package com.bw.module_mine

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.bw.lib_common.base.BaseMVIActivity
import com.bw.lib_common.utils.ToastUtils
import com.bw.lib_common.utils.ValidUtils
import com.bw.module_mine.databinding.ActivityRegisterBinding
import com.bw.module_mine.intent.MineIntent
import com.bw.module_mine.state.RegisterState
import com.bw.module_mine.viewmodel.MineViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class RegisterActivity : BaseMVIActivity() {
    private val binding : ActivityRegisterBinding by lazy {
        ActivityRegisterBinding.inflate(layoutInflater)
    }
    private lateinit var mineViewModel: MineViewModel
    override fun generateViewModel() {
        mineViewModel = ViewModelProvider(this)[MineViewModel::class.java]
    }
    override fun getLayoutRootView(): View {
        return binding.root
    }

    override fun handleState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED) {
                mineViewModel.registerState_.collect(){
                    when(it) {
                        is RegisterState.Init -> {

                        }
                        is RegisterState.Loading -> {

                        }
                        is RegisterState.Success -> {
                            showMsg("注册成功")
                            finish()
                        }
                        is RegisterState.Error -> {
                            showMsg("注册失败")
                        }
                        is RegisterState.Empty -> {

                        }
                    }
                }
            }
        }
    }

    override fun initEvent() {
        super.initEvent()

        binding.apply {
            tvGoLogin.setOnClickListener {
                finish()
            }

            btRegister.setOnClickListener {
                register()
            }
        }
    }

    private fun register() {
        val phone = binding.etUser.text.toString()
        val pass = binding.etPass.text.toString()
        val repass = binding.etRepass.text.toString()

        if (!ValidUtils.phoneNumberValid(phone)) {
            showMsg("账号格式错误")
            return
        }

        if (!ValidUtils.pwdValid(pass) || !ValidUtils.pwdValid(repass)) {
            showMsg("密码不能少于6位")
            return
        }

        if (pass != repass){
            showMsg("两次密码不一致")
            return
        }

        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED) {
                mineViewModel.intents.send(MineIntent.Register(phone,pass))
            }
        }
    }
}